package com.storlead.security.controller;

import com.alibaba.fastjson.JSONObject;
import com.storlead.security.model.SysUser;
import com.storlead.security.service.SysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: Timo
 * @Date: 2019/3/15 09:37
 * @Description:  首页控制器
 */
@RestController
@RequestMapping
public class IndexController {

    @Resource
    private SysUserService sysUserService;


    @GetMapping("/index")
    public JSONObject index(){
        JSONObject resultJson = new JSONObject();
        resultJson.put("index","欢饮回来！！！");

        return resultJson;
    }

    @GetMapping("/get")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    public JSONObject get(Integer userId){
        SysUser sysUser = sysUserService.findById(userId);
        JSONObject resultJson = (JSONObject) JSONObject.toJSON(sysUser);
        return resultJson;
    }

    @GetMapping("/get_user")
    @PreAuthorize("hasAuthority('ROLE_USER')")
    public JSONObject getUser(Integer userId){
        SysUser sysUser = sysUserService.findById(userId);
        JSONObject resultJson = (JSONObject) JSONObject.toJSON(sysUser);
        resultJson.put("role","user");
        return resultJson;
    }



}

